package com.hk.commons;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtil {
	private static final String password = "hX2znTnZ6LJg+A7v";
	
	static Cipher cipher;
	static final String KEY_ALGORITHM = "AES";
	
	
	static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding";
	/* 
     *  
     */
	static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
	

	/*******************************************************************************
	 * AES加解密算法
	 * 
	 * @author arix04
	 * 
	 */

	    // 加密
	    public static String Encrypt(String content ) throws Exception {
	        if (password == null) {
	            System.out.print("password 为空null");
	            return null;
	        }
	         
	        KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);  
            kgen.init(128, new SecureRandom(password.getBytes()));  
            SecretKey secretKey = kgen.generateKey();  
            byte[] enCodeFormat = secretKey.getEncoded();  
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);  
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);// 创建密码器  
           
            byte[] byteContent = content.getBytes("utf-8");  
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化  
            byte[] result = cipher.doFinal(byteContent);

	        return Base64.encodeBase64String( result);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
	    }

	    // 解密
	    public static String Decrypt(String content) throws Exception {
	        try {
	             
	            byte[] raw = password.getBytes("ASCII");
	            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
	            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);// 创建密码器  
	            /*IvParameterSpec iv = new IvParameterSpec("0102030405060708"
	                    .getBytes());
	            */
	            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	            byte[] encrypted1 = Base64.decodeBase64(content);//先用base64解密
	            try {
	                byte[] original = cipher.doFinal(encrypted1);
	                String originalString = new String(original);
	                return originalString;
	            } catch (Exception e) {
	                System.out.println(e.toString());
	                return null;
	            }
	        } catch (Exception ex) {
	            System.out.println(ex.toString());
	            return null;
	        }
	    }

	    public static void main(String[] args) throws Exception {
	        /*
	         * 加密用的Key 可以用26个字母和数字组成，最好不要用保留字符，虽然不会错，至于怎么裁决，个人看情况而定
	         * 此处使用AES-128-CBC加密模式，key需要为16位。
	         */
	        String cKey = "hX2znTnZ6LJg+A7v6w74YA==";
	        // 需要加密的字串
	        //String cSrc = "Email : arix04@xxx.com";
	        
	        String cSrc = "{\"orderId\":7553009397738,\"userId\":74991294905821,\"productId\":73936364615046,\"productCode\":\"HCDB006\",\"userName\":\"李培李培李培\",\"identity\":\"41018319890425103\",\"phoneNum\":\"13308338698\",\"paymentNo\":\"\",\"cbFunName\":\"buyCb\"}";  
	    	
	        
	        System.out.println(cSrc);
	        // 加密
	        long lStart = System.currentTimeMillis();
	        String enString = Encrypt(cSrc);
	        System.out.println("加密后的字串是：" + enString);

	        long lUseTime = System.currentTimeMillis() - lStart;
	        System.out.println("加密耗时：" + lUseTime + "毫秒");
	        // 解密
	        lStart = System.currentTimeMillis();
	        String DeString = Decrypt(enString);
	        System.out.println("解密后的字串是：" + DeString);
	        lUseTime = System.currentTimeMillis() - lStart;
	        System.out.println("解密耗时：" + lUseTime + "毫秒");
	    }
	}

	 
